/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.companypart.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import org.toools.gwt.widget.client.bc.BasicControl;
import org.toools.gwt.widget.client.bc.BasicListRenderer;
import org.toools.gwt.widget.client.bc.BasicTogglePanel;
import org.toools.gwt.widget.client.bc.DefaultCallback;
import org.toools.gwt.widget.client.bc.DefaultDeleteTogglePanel;
import org.toools.gwt.widget.client.bc.RelationControl;
import org.toools.gwt.widget.client.cloud.CloudListener;
import org.toools.gwt.widget.client.cloud.TagVO;
import org.toools.gwt.wmc.client.additive.AdditiveService;
import org.toools.gwt.wmc.client.additive.gui.AdditiveControl;
import org.toools.gwt.wmc.client.additive.gui.AdditivePanel;
import org.toools.gwt.wmc.client.application.ApplicationService;
import org.toools.gwt.wmc.client.application.gui.ApplicationControl;
import org.toools.gwt.wmc.client.application.gui.ApplicationPanel;
import org.toools.gwt.wmc.client.companypart.CompanyPartServiceAsync;
import org.toools.gwt.wmc.client.container.ContainerService;
import org.toools.gwt.wmc.client.container.gui.ContainerControl;
import org.toools.gwt.wmc.client.container.gui.ContainerPanel;
import org.toools.gwt.wmc.jpa.Additive;
import org.toools.gwt.wmc.jpa.Application;
import org.toools.gwt.wmc.jpa.CompanyPart;
import org.toools.gwt.wmc.jpa.Container;

/**
 *
 * @author Manuel
 */
public class CompanyPartListRenderer extends BasicListRenderer{

    private AdditiveControl adc = null;
    private ApplicationControl apc = null;
    private ContainerControl cc = null;

    private CompanyPart cp = null;

    BasicTogglePanel additives = new BasicTogglePanel("set additives", 1, true) {
        public void onActive() {
            clear();
            addChild(new AdditivesRelation((CompanyPart) getTag().getAttachedBusinessObject()));
        }
    };

    BasicTogglePanel applications = new BasicTogglePanel("set applications", 1, true) {
        public void onActive() {
            clear();
            addChild(new ApplicationsRelation((CompanyPart) getTag().getAttachedBusinessObject()));
        }
    };

    BasicTogglePanel containers = new BasicTogglePanel("set containers", 1, true) {
        public void onActive() {
            clear();
            addChild(new ContainersRelation((CompanyPart) getTag().getAttachedBusinessObject()));
        }
    };

    org.toools.gwt.widget.client.bc.ToggleButton additivetb = new org.toools.gwt.widget.client.bc.ToggleButton("additives", false) {
        public void toggle() {
            super.toggle();
            if(adc != null) {
                adc.setVisible(isOn());
            }
        }
    };

    org.toools.gwt.widget.client.bc.ToggleButton applicationtb = new org.toools.gwt.widget.client.bc.ToggleButton("applications", false) {
        public void toggle() {
            super.toggle();
            if(apc != null) {
                apc.setVisible(isOn());
            }
        }
    };

    org.toools.gwt.widget.client.bc.ToggleButton containertb = new org.toools.gwt.widget.client.bc.ToggleButton("containers", false) {
        public void toggle() {
            super.toggle();
            if(cc != null) {
                cc.setVisible(isOn());
            }
        }
    };

    public CompanyPartListRenderer(TagVO tag, BasicControl bc) {
		super(tag, bc);
        cp = (CompanyPart)tag.getAttachedBusinessObject();

        adc = new AdditiveControl(AdditiveService.Util.getInstance(),cp);
        apc = new ApplicationControl(ApplicationService.Util.getInstance(),cp);
        cc = new ContainerControl(ContainerService.Util.getInstance(),cp);

        adc.setVisible(false);
        apc.setVisible(false);
        cc.setVisible(false);

        addLeft(additivetb);
        addLeft(applicationtb);
        addLeft(containertb);
        
        addControl(adc);
        addControl(apc);
        addControl(cc);
        
        addButton(additives);
        addButton(applications);
        addButton(containers);
	}

    @Override
    public void setTag(TagVO tag) {
		CompanyPart compp = (CompanyPart) tag.getAttachedBusinessObject();
		setTitle(compp.getName());
	}

    public Widget getChildBasicPanel() {
        Object o = getBasicControl().getBasicPanel();
        if (o instanceof AdditivePanel) {
            AdditivePanel ap = (AdditivePanel) o;
            ap.addParent((CompanyPart)getTag().getAttachedBusinessObject());
            return ap;
        } else if (o instanceof ApplicationPanel) {
            ApplicationPanel ap = (ApplicationPanel) o;
            ap.addParent((CompanyPart)getTag().getAttachedBusinessObject());
            return ap;
        } else {
            ContainerPanel cpanel = (ContainerPanel) o;
            cpanel.addParent((CompanyPart)getTag().getAttachedBusinessObject());
            return cpanel;
        }
    }
    public String getExpandMessage() {
        String ret = "<div><img src='folder.png'/> " + cp.getAdditives().size() + " Additive childs</div>";
        ret += "<div><img src='folder.png'/> " + cp.getApplications().size() + " Application childs</div>";
        ret += "<div><img src='folder.png'/> " + cp.getContainers().size() + " Container childs</div>";
        return ret;
    }
    public boolean getExpandVisible() {
        return (( cp.getAdditives() != null && cp.getAdditives().size() > 0) || ( cp.getApplications() != null && cp.getApplications().size() > 0) || ( cp.getApplications() != null && cp.getContainers().size() > 0));
    }

    public DefaultDeleteTogglePanel getDefaultDeleteTogglePanel() {
        return new DefaultDeleteTogglePanel(getBasicControl(), getTag().getAttachedBusinessObject()) {
            @Override
            public boolean valid() {
                CompanyPart compp = (CompanyPart) getTag().getAttachedBusinessObject();
                if ( ( compp.getAdditives() != null && compp.getAdditives().size() > 0) || ( compp.getApplications() != null && cp.getApplications().size() > 0) || ( compp.getContainers() != null && cp.getContainers().size() > 0)){
                    return false;
                } else {
                    return true;
                }
            }
            public String getDeleteConfirmMessage() {
                if(!valid())
                    return "deletion not possible with childs.";
                else
                    return getBasicControl().getDeleteConfirmMessage();
            }
        };
    }

    public class AdditivesRelation extends RelationControl {
        CompanyPart cp;
        public AdditivesRelation(CompanyPart cp) {
            super(false, true, true, false);
            this.cp = cp;
            AdditiveService.Util.getInstance().getAdditives(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }
        public TagVO getTagVO(Object bp) {
            Additive a = (Additive) bp;
            TagVO result = new TagVO("" + a.getId(), a.getContent(), 0, 0);
            result.setAttachedBusinessObject(a);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<Additive>)result).toArray()));
                if((cp.getAdditives() != null) && (cp.getAdditives().size() > 0)) {
                    String[] active = new String[cp.getAdditives().size()];
                    int j = 0;
                    for(Iterator i = cp.getAdditives().iterator(); i.hasNext();) {
                        Additive a = (Additive) i.next();
                        active[j++] = "" + a.getId();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                Collection<Additive> additives = new Vector<Additive>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    additives.add((Additive)tag.getAttachedBusinessObject());
                }
                cp.setAdditives(additives);
                ((CompanyPartServiceAsync)getBasicControl().getService()).update(cp, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }

    public class ApplicationsRelation extends RelationControl {
        CompanyPart cp;
        public ApplicationsRelation(CompanyPart cp) {
            super(false, true, true, false);
            this.cp = cp;
            ApplicationService.Util.getInstance().getApplications(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }
        public TagVO getTagVO(Object bp) {
            Application a = (Application) bp;
            TagVO result = new TagVO("" + a.getId(), a.getContent(), 0, 0);
            result.setAttachedBusinessObject(a);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<Application>) result).toArray()));
                if((cp.getApplications() != null) && (cp.getApplications().size() > 0)) {
                    String[] active = new String[cp.getApplications().size()];
                    int j = 0;
                    for(Iterator i = cp.getApplications().iterator(); i.hasNext();) {
                        Application a = (Application) i.next();
                        active[j++] = "" + a.getId();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                Collection<Application> application = new Vector<Application>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    application.add((Application)tag.getAttachedBusinessObject());
                }
                cp.setApplications(application);
                ((CompanyPartServiceAsync)getBasicControl().getService()).update(cp, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }

    public class ContainersRelation extends RelationControl {
        CompanyPart cp;
        public ContainersRelation(CompanyPart cp) {
            super(false, true, true, false);
            this.cp = cp;
            ContainerService.Util.getInstance().getContainers(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }
        public TagVO getTagVO(Object bp) {
            Container a = (Container) bp;
            TagVO result = new TagVO("" + a.getId(), a.getName(), 0, 0);
            result.setAttachedBusinessObject(a);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<Container>) result).toArray()));
                if((cp.getContainers() != null) && (cp.getContainers().size() > 0)) {
                    String[] active = new String[cp.getContainers().size()];
                    int j = 0;
                    for(Iterator i = cp.getContainers().iterator(); i.hasNext();) {
                        Container a = (Container) i.next();
                        active[j++] = "" + a.getId();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                Collection<Container> containers = new Vector<Container>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    containers.add((Container)tag.getAttachedBusinessObject());
                }
                cp.setContainers(containers);
                ((CompanyPartServiceAsync)getBasicControl().getService()).update(cp, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }

}
